//
//  KakaFollowViewController.swift
//  NewsItem
//
//  Created by mac on 2023/7/13.
//  Copyright © 2023 Kaka. All rights reserved.
//

import UIKit

class Pet_FollowViewController: BasicViewController,NaviBarHidden {

    let titleV = UIView.init(frame: .init(x: 0, y: 0, width:250.Fit, height: 44))
    let careB = UIButton.initV(font: .setTypeFace(18,type: .medium), text: "FOLLOWING", img: "",contentMent: .center)
    let fansB = UIButton.initV(font: .setTypeFace(18,type: .medium), text: "FOLLOWED", img: "",contentMent: .center)
    let leftB = UIButton.initV(img: "icon_return 1")
    
    private lazy var listView: UICollectionView = {
        let layout = UICollectionViewFlowLayout()
        layout.minimumLineSpacing = 5.Fit
        layout.minimumInteritemSpacing = 5.Fit
        layout.itemSize = .init(width: screenWidth/2 - 40/2, height: 200.Fit)
        let view = UICollectionView(frame: .zero, collectionViewLayout: layout)
        view.register(User_Item.self, forCellWithReuseIdentifier: User_Item.description())
        view.backgroundColor = .clear
        view.contentInset = .init(top: 0, left: 15.Fit, bottom: 0, right: 15.Fit)
        view.delegate = self
        view.dataSource = self
        view.bounces = false
        return view
    }()

    var type = 0
    var datasource:[InfoUtil.initInfo?] = []
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
        view.subviews {
            titleV.subviews {
                leftB
                careB
                fansB
            }
            listView
        }
        
        careB.setTitleColor(.color(hexString: "#1D0B00",alpha: 0.38), for: .normal)
        fansB.setTitleColor(.color(hexString: "#1D0B00",alpha: 0.38), for: .normal)

        careB.setTitleColor(.color(hexString: "#1D0B00"), for: .selected)
        fansB.setTitleColor(.color(hexString: "#1D0B00"), for: .selected)
        
        titleV.left(0).right(0).top(StatusBarHeight).height(44.Fit)
        listView.left(0).right(0).bottom(0)
        listView.Top == titleV.Bottom
        
        leftB.left(0).top(0).height(44).width(65.Fit)
        leftB.addAct {
            self.navigationController?.popViewController(animated: true)
        }
        
        careB.top(0).height(44).width(125)
        careB.Leading == leftB.Trailing + 10.Fit
        fansB.top(0).height(44).width(115).right(25.Fit)
        
        if type == 0{
            careB.isSelected = true
        }else{
            fansB.isSelected = true
        }
        careB.addAct {
            self.careB.isSelected = true
            self.fansB.isSelected = false
            self.type = 0
            self.loadData()
        }
        fansB.addAct {
            self.careB.isSelected = false
            self.fansB.isSelected = true
            self.type = 1
            self.loadData()
        }
        self.loadData()
        listView.emptyGameView(.noList)
    }
    
    func loadData() -> Void {
        if type == 0{
            RT.FriendListAction { list in
                self.datasource = list
                self.listView.reloadData()
            }
        }else{
            RT.fansListAction { list in
                self.datasource = list
                self.listView.reloadData()
            }
        }
    }
}

extension Pet_FollowViewController: UICollectionViewDelegate, UICollectionViewDataSource {
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        self.datasource.count
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: User_Item.description(), for: indexPath) as! User_Item
        cell.model = self.datasource[indexPath.row]
        cell.removeB.addAct {
            Toast_View_Tool.show()
            if self.type == 0{
                print("FriendListAction========   ")
                RT.AddDeleteFriend(idstring: self.datasource[indexPath.row]?.id ?? "", type: "0") { s in
                    Toast_View_Tool.dismiss()
                    self.datasource.remove(at: indexPath.row)
                    self.listView.reloadData()
                }
            }else
            {
                print("fansListAction========   ")
                RT.AddDeleteFriend(idstring: self.datasource[indexPath.row]?.id ?? "", type: "1") { s in
                    Toast_View_Tool.dismiss()
                    self.listView.reloadData()
                }
            }
        }
        return cell
    }
}

class User_Item: UICollectionViewCell {
    
    let coverI = UIImageView.initImgView(img: "")
    let nameL = UILabel.initV(UIFont.setTypeFace(16,type: .medium), text: "", textColor: .white)
    let avarB = UIButton.initV(img: "avarB")
    var model:InfoUtil.initInfo?{
        didSet{
            self.nameL.text = model?.name
            if let url = URL(string: self.model?.headImage ?? "") {
                self.avarB.kf.setImage(with: url, for: .normal,placeholder: .init(named: "avarB"))
            }
        }
    }
    var anchormodel:InfoUtil.anchorPageList?{
        didSet{
            self.nameL.text = anchormodel?.name
            if let url = URL(string: anchormodel?.headImage ?? "") {
                self.avarB.kf.setImage(with: url, for: .normal,placeholder: .init(named: "avarB"))
            }
        }
    }
    
    lazy var removeB: UIButton = {
        let button = UIButton(type: .custom)
        button.titleLabel?.font = .setTypeFace(16.Fit,type: .medium)
        button.setTitleColor(mainColor, for: .normal)
        button.setTitle("Remove", for: .normal)
        button.backgroundColor = .black
        return button
    }()

    override init(frame: CGRect) {
        super.init(frame: frame)
        contentView.subviews {
            coverI.subviews {
                avarB
                nameL
                removeB
            }
        }
        coverI.fillContainer()
        coverI.setAllCorners(corner: 20.Fit)
        coverI.backgroundColor = .white
        coverI.isUserInteractionEnabled = true
        
        avarB.top(15.Fit).size(70.Fit)
        avarB.setAllCorners(corner: 35.Fit)
        avarB.centerHorizontally()
        
        nameL.textColor = .black
        nameL.height(25.Fit).right(15.Fit).left(15.Fit)
        nameL.Top == avarB.Bottom + 10.Fit
        nameL.textAlignment = .center
        
        removeB.left(15.Fit).right(15.Fit).height(40.Fit).bottom(20.Fit)
        removeB.setAllCorners(corner: 20.Fit)
        removeB.addAct {
            
        }
        
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

}




class fansCell: UITableViewCell {
    
    let backV = UIView.init(frame: .zero)
    let headBtn = UIButton.initV(font: .setTypeFace(20, type: .bold), text: "", img: "placeavar",contentMent: .left)
    private var nameL = UILabel.initV(.setTypeFace(18, type: .bold), text: "", textColor: mainColor)
    private var descL = UILabel.initV(.setTypeFace(13.Fit, type: .medium), text: "", textColor: mainColor)
    let more = UIButton.initV(font: .setTypeFace(16, type: .bold), text: "Remove", img: "",contentMent: .center)
    var model:InfoUtil.initInfo?{
        didSet{
            self.nameL.text = model?.name
            if let url = URL(string: model?.headImage ?? "") {
                self.headBtn.kf.setImage(with: url, for: .normal)
            }
            self.descL.text = model?.userDesc
        }
    }
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        backgroundColor = .clear
        selectionStyle = .none
        contentView.subviews {
            backV.subviews {
                headBtn
                nameL
                descL
                more
            }
        }
        
        backV.isUserInteractionEnabled = true
        backV.left(15.Fit).top(15.Fit).right(15.Fit).height(80.Fit)
        backV.backgroundColor = .color(hexString: "#EDF2DC")
        backV.setAllCorners(corner: 14.Fit)

        headBtn.left(15.Fit).top(15.Fit).width(50.Fit).height(50.Fit)
        headBtn.setAllCorners(corner: 25.Fit)
        
        more.trailing(15).height(40.Fit).width(90.Fit)
        more.centerVertically()
        more.backgroundColor = mainColor
        more.setAllCorners(corner: 10.Fit)
        more.setTitleColor(.white, for: .normal)
        
        nameL.top(20.Fit).height(20.Fit)
        nameL.Leading == headBtn.Trailing + 10.Fit
        nameL.Trailing == more.Leading - 10.Fit

        descL.height(20.Fit)
        descL.Leading == headBtn.Trailing + 10.Fit
        descL.Trailing == more.Leading - 10.Fit
        descL.Top == nameL.Bottom + 5.Fit

    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

}


